package com.servlet;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.UserDao;
import com.model.User;
import com.util.DbUtil;
/**
 * @author wangjg
 * @date 2023/11/1 15:15
 * @Description: 密码servlet
 */
@WebServlet(name = "PasswordServlet")
public class PasswordServlet extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
    UserDao userDao = new UserDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        //修改密码页面调整
        if("preChange".equals(action)) {
            passwordPreChange(request, response);
        } else if("change".equals(action)) {
            //修改密码
            passwordChange(request, response);
        }
    }

    private void passwordChange(HttpServletRequest request,
                                HttpServletResponse response)throws ServletException, IOException {
        HttpSession session = request.getSession();
        Object currentUserType = session.getAttribute("currentUserType");
        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        Connection con = null;
        try {
            con = dbUtil.getCon();
            //管理员修改密码
            User user = (User)(session.getAttribute("currentUser"));
            if("admin".equals(currentUserType)) {
                if(oldPassword.equals(user.getPassword())) {
                    userDao.updatePassword(con, user.getId(), newPassword);
                    user.setPassword(newPassword);
                    request.setAttribute("oldPassword", oldPassword);
                    request.setAttribute("newPassword", newPassword);
                    request.setAttribute("rPassword", newPassword);
                    request.setAttribute("error", "修改成功 ");
                    request.getRequestDispatcher("login.jsp").forward(request, response);
                } else {
                    request.setAttribute("oldPassword", oldPassword);
                    request.setAttribute("newPassword", newPassword);
                    request.setAttribute("rPassword", newPassword);
                    request.setAttribute("error", "原密码错误");
                    request.setAttribute("mainPage", "admin/passwordChange.jsp");
                    request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
                }
                //学生修改密码
            }else if("student".equals(currentUserType)) {
                if(oldPassword.equals(user.getPassword())) {
                    userDao.updatePassword(con, user.getId(), newPassword);
                    user.setPassword(newPassword);
                    request.setAttribute("oldPassword", oldPassword);
                    request.setAttribute("newPassword", newPassword);
                    request.setAttribute("rPassword", newPassword);
                    request.setAttribute("error", "修改成功 ");
                    request.getRequestDispatcher("login.jsp").forward(request, response);
                } else {
                    request.setAttribute("oldPassword", oldPassword);
                    request.setAttribute("newPassword", newPassword);
                    request.setAttribute("rPassword", newPassword);
                    request.setAttribute("error", "原密码错误");
                    request.setAttribute("mainPage", "student/passwordChange.jsp");
                    request.getRequestDispatcher("mainStudent.jsp").forward(request, response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void passwordPreChange(HttpServletRequest request,
                                   HttpServletResponse response)throws ServletException, IOException {

        HttpSession session = request.getSession();
        Object currentUserType = session.getAttribute("currentUserType");
        if("admin".equals(currentUserType)) {
            request.setAttribute("mainPage", "admin/passwordChange.jsp");
            request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
        }else if("student".equals(currentUserType)) {
            request.setAttribute("mainPage", "student/passwordChange.jsp");
            request.getRequestDispatcher("mainStudent.jsp").forward(request, response);
        }
    }
}
